# -*- coding: utf-8 -*-
import sys

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.Qt import *
from PyQt5.QtGui import *

from client.pages.RegisterWin import *
from client.func.VerifiCode import random_code, create_verificode, save_img
from client.func.ClientHttp import register_post

root = "./"


class RegisterPage(QMainWindow, Ui_RegisterWin):
    """注册界面"""
    login_signal = pyqtSignal()
    def __init__(self):
        super(RegisterPage, self).__init__()
        self.setupUi(self)
        self.code = ""
        self.OnverifiCode()
        self.setWindowFlags(Qt.WindowStaysOnTopHint | Qt.FramelessWindowHint)
        self.pswd_text.setEchoMode(QtWidgets.QLineEdit.Password)
        self.sure_pswd_text.setEchoMode(QtWidgets.QLineEdit.Password)
        self.info_label.setText("")

        self.eye_button1.pressed.connect(self.EyePressed1)
        self.eye_button1.released.connect(self.EyeRelease1)
        self.eye_button2.pressed.connect(self.EyePressed2)
        self.eye_button2.released.connect(self.EyeRelease2)
        self.close_button.clicked.connect(self.OnClose)
        self.small_button.clicked.connect(self.OnSmall)
        self.verify_button.clicked.connect(self.OnverifiCode)
        self.register_button.clicked.connect(self.OnRegister)

    def formatCheck(self):
        if not self.name_text.text():
            self.info_label.setText(u"昵称不能为空！")
            return False
        elif not self.user_text.text():
            self.info_label.setText(u"用户名不能为空！")
            return False
        elif not self.user_text.text().isdigit():
            self.info_label.setText(u"用户名必须为纯数字！")
            return False
        elif not self.pswd_text.text():
            self.info_label.setText(u"密码不能为空！")
            return False
        elif not self.pswd_text.text() == self.sure_pswd_text.text():
            self.info_label.setText(u"两次密码必须一致！")
            return False
        elif not self.code == self.verify_text.text():
            self.info_label.setText(u"验证码错误！")
            self.OnverifiCode()
            return False
        else:
            self.info_label.setText(u"")
            return True

    def OnRegister(self):
        if not self.formatCheck():
            return
        self.info_label.setText(u"注册中...")
        status, info = register_post(self.user_text.text(), self.pswd_text.text(), self.name_text.text())
        if status:
            self.info_label.setText(info)
        else:
            self.info_label.setText(info)

    def OnverifiCode(self):
        img_name = root + "client/.cache/code.jpg"
        self.code = random_code()
        img = create_verificode(35, 101, self.code)
        save_img(img, img_name)
        jpg = QtGui.QPixmap(img_name).scaled(self.verify_img.width(), self.verify_img.height())
        self.verify_img.setPixmap(jpg)

    def EyePressed1(self):
        self.pswd_text.setEchoMode(QtWidgets.QLineEdit.Normal)

    def EyeRelease1(self):
        self.pswd_text.setEchoMode(QtWidgets.QLineEdit.Password)

    def EyePressed2(self):
        self.sure_pswd_text.setEchoMode(QtWidgets.QLineEdit.Normal)

    def EyeRelease2(self):
        self.sure_pswd_text.setEchoMode(QtWidgets.QLineEdit.Password)

    def OnSmall(self):
        self.showMinimized()

    def OnClose(self):
        self.login_signal.emit()
        self.close()


if __name__ == "__main__":
    root = "../../"
    app = QApplication(sys.argv)
    myWin = RegisterPage()
    myWin.show()
    sys.exit(app.exec_())
